<?php
namespace app\admin\controller;
use pidan\App;
use pidan\facade\Request;
use app\admin\Controller;
use app\admin\model\Admin as AdminModel;
use pidan\facade\Db;
use pidan\facade\Validate;




class Admin extends Controller
{
	protected function initialize()
	{
		$this->model=new AdminModel;
	}
	//取   admin_name
	public function session(){
		$tmp['admin_id']=token_session('admin_id');
		$tmp['admin_username']=token_session('admin_username');
		return $this->result(['code'=>0,'msg'=>'','data'=>$tmp]);
	}
	public function list(){
		$req=$this->request;
		$table=$this->model;
		//查询字段
		$data=$req->only(['username','mobile','email'],'get','trim');

		//要显示的
		$filed=explode(',','id,username,email,mobile,logintime,loginip,createtime,status');

		$sort_by=input('request.sort_by','id','trim');
		$sort_order=input('request.sort_order','asc','trim');

		$page=input('request.page',1,'intval');
		$limit=input('request.limit',30,'intval');

		$list= $table->field($filed);
		foreach($data as $k=>$v){
			if(empty($v))  unset($data[$k]);
		}

		$count=$list->count('id');
		$list=$list->where($data)->order($sort_by, $sort_order)->limit($limit)->page($page)->select()->toArray();
		return $this->lists($list,$count);
	}
	public function add(){//username: '13555555555', mobile: '13555555555', email: '13555555555@sfasd.com', password: '13555555555', role: '0'
		$app=$this->app;
		$req=$this->request;

		$data=$req->only(['username','mobile','email','password'],'post');
		$data['status']=$req->post('status','')=='on' ? 'normal' :'hidden';

		$data['salt'] = substr(md5(microtime(true)),0,6);
		$data['password'] = md5(md5($data['password']) . $data['salt']);
		$data['nickname']=$data['username'];
		$data['avatar']='/static/img/avatar/12.jpg';

		Db::startTrans();
		try {
				$validate = new \app\admin\validate\Admin;
				if(!$validate->scene('add')->check($data)){
						$result=['code'=>1,'msg'=>$validate->getError(),'data'=>''];// 验证失败 输出错误信息
				}
				else{
						(new AdminModel)->save($data);
						$this->adminLog('添加管理员:'.$data['username']);
						$result=['code'=>0,'msg'=>'操作成功','data'=>''];
				}
				Db::commit();
		} catch (\Exception $e) {
				Db::rollback();
				$result=['code'=>2,'msg'=>$e->getMessage(),'data'=>''];

		}
		return $this->result($result);
	}
	public function edit(){
		$req=$this->request;
		$data=$req->only(['id','username','mobile','email','password'],'post');
		$data['status']=$req->post('status','')=='on' ? 'normal' :'hidden';

		$data['nickname']=$data['username'];

		$validate = new \app\admin\validate\Admin;

		if (!empty($data['password']) && !$validate->is($data['password'], '\S{6,30}')) {
				$result=['code'=>1,'msg'=>lang("Please input correct password"),'data'=>''];
				return $this->result($result);
		}

		if (!empty($data['password'])) {
				$data['salt'] = substr(md5(microtime(true)),0,6);
				$data['password'] = md5(md5($data['password']) . $data['salt']);
		} else {
				unset($data['password']);
		}

		Db::startTrans();
		try {
				$validate->rule([
						'username' => 'require|regex:\w{3,30}|unique:admin,username,' . $data['id'],
						'email'    => 'require|email|unique:admin,email,' . $data['id'],
						'mobile'    => 'regex:1[3-9]\d{9}|unique:admin,mobile,' . $data['id'],
						'password' => 'regex:\S{32}',
				]);
				if(!$validate->scene('edit')->check($data)){
						$result=['code'=>2,'msg'=>$validate->getError(),'data'=>''];// 验证失败 输出错误信息
				}
				else{
						$admin = AdminModel::find($data['id']);
						$admin->save($data);
						$this->adminLog('编辑管理员:'.$data['username'].$data['email'].$data['mobile']);
						$result=['code'=>0,'msg'=>'操作成功','data'=>''];
				}
				Db::commit();
		} catch (\Exception $e) {
				Db::rollback();
				$result=['code'=>3,'msg'=>$e->getMessage(),'data'=>''];

		}
		return $this->result($result);
	}
	public function del(){
		$row= AdminModel::find(input('request.id',0,'intval'));
		if($row->id==1){
				$result=['code'=>1,'msg'=>'超级管理不能删除'];
		}
		elseif(token_session('admin_id')==$row->id){
				$result=['code'=>1,'msg'=>'不能删除自己'];
		}
		else{
				$row->delete();
				$row=$row->toArray();
				$this->adminLog('删除管理员:'.$row['username']);
				$result=['code'=>0,'msg'=>''];
		}

		return $this->result($result);
	}
}
